<template>
  <div class="app-container">
    <!-- 页面头部 -->
    <div class="page-header">
      <!-- <router-link class="back-link" to="/main/renshi/renshizhidu/jichabiao/jichabiao">
        <i class="el-icon-arrow-left"></i>
        返回
      </router-link> -->
      <el-button type="primary" style="margin-right: 20px;" @click="$router.push('/main/renshi/renshizhidu/jichabiao/jichabiao')" icon="el-icon-back">返回</el-button>
      <h2>文件/资料稽查表列表</h2>
    </div>
    <!-- 筛选容器 -->
    <div class="filter-container">
      <el-input v-model="listQuery.keyword" placeholder="请输入关键字" style="width: 196px;" class="filter-item"
        @keyup.enter.native="handleFilter" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-plus" @click="handleCreate">
        新增
      </el-button>
    </div>

    <!-- 表格 -->
    <div style="padding-bottom: 20px;">
      <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
        <el-table-column type="selection" width="55" />
        <el-table-column label="序号" type="index" width="50" align="center" />
        <el-table-column label="稽查时间" width="180" align="center">
          <template slot-scope="scope">
            {{ scope.row.inspectionTime | parseTime }}
          </template>
        </el-table-column>
        <el-table-column label="稽查部门" width="180" align="center">
          <template slot-scope="scope">
            {{ scope.row.inspectionDepartment }}
          </template>
        </el-table-column>
        <el-table-column label="主要稽查项目">
          <template slot-scope="scope">
            {{ scope.row.mainInspectionItems }}
          </template>
        </el-table-column>
        <el-table-column label="稽查状况描述">
          <template slot-scope="scope">
            {{ scope.row.inspectionStatusDescription }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="230">
          <template slot-scope="{row}">
            <el-button type="primary" size="mini" @click="handleView(row)">
              查看
            </el-button>
            <el-button type="success" size="mini" @click="handleUpdate(row)">
              编辑
            </el-button>
            <el-button type="danger" size="mini" @click="handleDelete(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import { getInspectionPage, deleteInspection, batchDeleteInspection } from '@/api/inspection'
import Pagination from '@/components/Pagination'

export default {
  name: 'InspectionList',
  components: { Pagination },
  filters: {
    parseTime(time) {
      return time ? new Date(time).toLocaleString() : ''
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        keyword: ''
      },
      selectedRows: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getInspectionPage(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.pageNum = 1
      this.getList()
    },
    handleSelectionChange(val) {
      this.selectedRows = val
    },
    handleCreate() {
      this.$router.push('/main/renshi/renshizhidu/jichabiao/jichabiao')
    },
    handleView(row) {
      this.$router.push({
        path: '/main/renshi/renshizhidu/jichabiao/jichabiao',
        query: { id: row.id, type: 'view' }
      })
    },
    handleUpdate(row) {
      this.$router.push({
        path: '/main/renshi/renshizhidu/jichabiao/jichabiao',
        query: { id: row.id, type: 'edit' }
      })
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteInspection(row.id).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
        })
      }).catch(() => {
        // 用户取消操作，这里可以添加一些提示信息，也可以不做处理
        this.$message({
          type: 'info',
          message: '操作已取消'
        })
      })
    },
    handleBatchDelete() {
      const ids = this.selectedRows.map(row => row.id)
      if (ids.length === 0) {
        this.$message.warning('请选择要删除的记录')
        return
      }
      this.$confirm(`此操作将永久删除这${ids.length}条记录, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        batchDeleteInspection(ids).then(() => {
          this.$message({
            type: 'success',
            message: '批量删除成功!'
          })
          this.getList()
        })
      })
    }
  }
}
</script>

<style scoped>
/* 容器样式 */
.app-container {
  padding: 20px;
  /* background-color: #f5f7fa; */
  border-radius: 8px;
  /* box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); */
  min-height: calc(100vh - 40px);
  width: 1200px;
margin: 0 auto;
}

/* 页面头部样式 */
.page-header {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  background-color: #fff;
  padding: 0px 24px;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
}

/* 返回链接样式 */
.back-link {
  display: flex;
  align-items: center;
  text-decoration: none;
  color: #409EFF;
  font-size: 16px;
  margin-right: 20px;
  transition: all 0.3s;
}

.back-link i {
  margin-right: 5px;
}

.back-link:hover {
  opacity: 0.8;
}

/* 标题样式 */
.page-header h2 {
  margin: 0;
  font-size: 22px;
  font-weight: 500;
  color: #303133;
}

.filter-container {
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
  background-color: #fff;
  padding: 20px 0px;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
}

.filter-item {
  margin-right: 10px;
}

/* 按钮样式 */
.el-button {
  border-radius: 4px;
  padding: 8px 13px;
  font-family: '宋体';
  font-size: 1em;
}
</style>